//
//  SubjectVC.m
//  ErWa
//
//  Created by 赵颜彬 on 16/3/9.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SubjectVC.h"
#import "SubjectCell.h"
#import "SubjectModel.h"
#import <UIImageView+WebCache.h>
#import "AFNetworking.h"
#import "SubjectDetailPageVC.h"
#import "NewNestHeader.h"
@interface SubjectVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger number; /**< 用来保存加载新数据的动态值 */
@end

@implementation SubjectVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // navigation
    self.navigationItem.title = @"专题";
    
    [self dataHandler];
    
    [self createSubviews];
 

}
/// 数据处理
- (void)dataHandler
{
    //tableview数据
    NSString *str = [NSString stringWithFormat:@"http://appapi.17house.com/xiaoguotuApi.php?action=albumList&page=1&pageSize=10&tagid=1"];
    [AFNetworking GETWithURL:str completion:^(id result) {
        NSMutableArray *arr = result[@"data"];
        self.dataArr = [SubjectModel modelHanderWithArray:arr];
        [_tableview reloadData];
        NSLog(@"%@",self.dataArr);
    }];
}

/// 创建视图
- (void)createSubviews
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = [UIColor clearColor];
    [_tableview registerClass:[SubjectCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
}

/// 设置cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

/// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // 消除点击cell的灰色
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    // 判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        cell.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
    } else {
        cell.backgroundColor = [UIColor clearColor];
        
    }
  
    if (_dataArr.count != 0) {
        SubjectModel *m = _dataArr[indexPath.row];
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.url] placeholderImage:[UIImage imageNamed:@"heng"]];
        cell.contentLabel.text = m.desc;
        cell.likeImg.image = [UIImage imageNamed:@"huiaixin@2x"];
        cell.likeLabel.text = m.like_num;
        cell.commentImg.image = [UIImage imageNamed:@"huicomment@2x"];cell.commentLabel.text = m.comment_count;
        return cell;
    }else{
        return cell;
    }
}

/// 设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 2.5;
}

/// 点击cell进入详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectDetailPageVC *detailVC = [[SubjectDetailPageVC alloc] init];

    SubjectModel *m = _dataArr[indexPath.row];
    detailVC.idStr = m.detailId;
    //将当前的modal,传递给详情页面
    detailVC.mainModal = m;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
